package com.eastfair.builder.service.impl;

import com.eastfair.builder.service.BuilderHomeBondManageService;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.venuebooking.api.HomeBondManageFeign;
import com.eastfair.venueservice.enumeration.BondManageOrderStatusEnum;
import com.eastfair.venueservice.enumeration.ServiceOrderPayStatusEnum;
import com.eastfair.venueservice.enums.BondManagePayStatusEnum;
import com.eastfair.venueservice.vo.BondManageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 保证金订单服务
 *
 * @author dqq
 * @date 2023 /6/4
 */
@Service
@Slf4j
public class BuilderHomeBondManageServiceImpl implements BuilderHomeBondManageService {
    
    @Resource
    private HomeBondManageFeign homeBondManageFeign;
    
    @Override
    public List<BondManageVO> queryByExhibitorIdList(List<Long> exhibitorIdList) {
        R<List<BondManageVO>> result = homeBondManageFeign.queryByExhibitorIdList(exhibitorIdList);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public Map<Long, String> getBondPayStatus(List<Long> exhibitorIdList) {
        List<BondManageVO> bondManageList = queryByExhibitorIdList(exhibitorIdList);
        if (bondManageList == null || bondManageList.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<Long, List<BondManageVO>> bondMap = bondManageList
                .stream()
                .collect(Collectors.groupingBy(BondManageVO::getExhibitorId));
        Map<Long, String> bondPayStatusMap = new HashMap<>(16);
        for (Map.Entry<Long, List<BondManageVO>> entry : bondMap.entrySet()) {
            Long exhibitorId = entry.getKey();
            List<BondManageVO> childBondManageList = entry.getValue();
            //  没有保证金订单，设为未支付
            if (childBondManageList == null || childBondManageList.isEmpty()) {
                bondPayStatusMap.put(exhibitorId, ServiceOrderPayStatusEnum.NOT_PAY.getValue());
                continue;
            }
            boolean existHasPay = false;
            boolean existHasCancel = false;
            // 该展位下有没有被取消的和被支付的
            for (BondManageVO bondManageVO: childBondManageList) {
                 if (BondManagePayStatusEnum.PAYED.getCode().equals(bondManageVO.getPayStatus())) {
                     existHasPay = true;
                 }
                 if (BondManageOrderStatusEnum.CANCEL.eq(bondManageVO.getOrderStatus())) {
                     existHasCancel = true;
                 }
            }
            // 如果有被主场取消的，或者是被支付的，则认为已缴纳
            if (existHasPay || existHasCancel) {
                bondPayStatusMap.put(exhibitorId, ServiceOrderPayStatusEnum.HAS_PAY.getValue());
            } else {
                bondPayStatusMap.put(exhibitorId, ServiceOrderPayStatusEnum.NOT_PAY.getValue());
            }
        }
        return bondPayStatusMap;
    }
}